﻿// GlueTestDlg.cpp: 实现文件
//

#include "pch.h"
#include "GlueTestDlg.h"
#include "afxdialogex.h"


// CGlueTestDlg 对话框

IMPLEMENT_DYNAMIC(CGlueTestDlg, CDialogEx)

CGlueTestDlg::CGlueTestDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_GLUE_TEST, pParent)
	, m_nMinCircle(0)
	, m_nMaxCircle(0)
	, m_nCannyX(0)
	, m_nCannyY(0)
	, m_nDilateX(0)
	, m_nDeviation(0)
	, m_nWideCell(0)
	, m_nWidthFloor(0)
	, m_bSaveImage(FALSE)
	, m_bSaveImageOrg(FALSE)
	, m_bOffline(FALSE)
	, m_nCenterX(0)
	, m_nCenterY(0)
	, m_nThreShold(0)
	, m_nDilateY(0)
	, m_nBreakSpace(0)
	, m_nTestTimeout(0)
	, m_nThreShold2(0)
	, cnt(0)
	, UserSetCircle(FALSE)
	, SetCircleX(0)
	, SetCircleY(0)
	, SetCircleRMin(0)
	, SetCircleRMax(0)
	, TestCnt(0)
{

}

CGlueTestDlg::~CGlueTestDlg()
{
}

void CGlueTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_nMinCircle);
	DDX_Text(pDX, IDC_EDIT3, m_nMaxCircle);
	DDX_Text(pDX, IDC_EDIT6, m_nDilateX);
	DDX_Text(pDX, IDC_EDIT10, m_nDeviation);
	DDX_Text(pDX, IDC_EDIT11, m_nWideCell);
	DDX_Text(pDX, IDC_EDIT12, m_nWidthFloor);
	DDX_Check(pDX, IDC_CHECK_SAVE_IMAGE, m_bSaveImage);
	DDX_Check(pDX, IDC_CHECK_SAVE_IMAGE_ORG, m_bSaveImageOrg);
	DDX_Check(pDX, IDC_CHECK_OFFLINE, m_bOffline);
	DDX_Text(pDX, IDC_EDIT_CIRCLE_CENTERX, m_nCenterX);
	DDX_Text(pDX, IDC_EDIT_CIRCLE_CENTERY, m_nCenterY);
	DDX_Text(pDX, IDC_EDIT_THRESHOLD, m_nThreShold);
	DDX_Text(pDX, IDC_EDIT_CANNY_X, m_nCannyX);
	DDX_Text(pDX, IDC_EDIT_CANNY_Y, m_nCannyY);
	DDX_Text(pDX, IDC_EDIT14, m_nDilateY);
	DDX_Text(pDX, IDC_EDIT_GLUE_BRAK, m_nBreakSpace);
	DDX_Text(pDX, IDC_EDIT15, m_nTestTimeout);
	DDX_Text(pDX, IDC_EDIT_THRESHOLD2, m_nThreShold2);
	DDX_Text(pDX, IDC_EDIT2, cnt);
	DDX_Check(pDX, IDC_CHECK2, UserSetCircle);
	DDX_Text(pDX, IDC_EDIT_CIRCLE_X, SetCircleX);
	DDX_Text(pDX, IDC_EDIT_CIRCLE_Y, SetCircleY);
	DDX_Text(pDX, IDC_EDIT_CIRCLE_R1, SetCircleRMin);
	DDX_Text(pDX, IDC_EDIT_CIRCLE_R2, SetCircleRMax);
	DDX_Text(pDX, IDC_EDIT_THRESHOLD3, TestCnt);
}


BEGIN_MESSAGE_MAP(CGlueTestDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_SET_SAVE, &CGlueTestDlg::OnBnClickedButtonSetSave)
	ON_BN_CLICKED(IDC_BUTTON_HAND, &CGlueTestDlg::OnBnClickedButtonHand)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, &CGlueTestDlg::OnBnClickedButtonOpen)
	ON_BN_CLICKED(IDC_CHECK_OFFLINE, &CGlueTestDlg::OnBnClickedCheckOffline)
	ON_BN_CLICKED(IDC_BUTTON2, &CGlueTestDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON1, &CGlueTestDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CGlueTestDlg 消息处理程序

BOOL CGlueTestDlg::OnInitDialog()
{
	ReadConfigToEditValue();

	return TRUE;
}

void CGlueTestDlg::OnBnClickedButtonSetSave()
{
	ReadEditValueToConfig();
}

void CGlueTestDlg::ReadEditValueToConfig()
{
	UpdateData(TRUE);

	gSystemMgr.m_sGlueTest.m_nMinCircle = m_nMinCircle;
	gSystemMgr.m_sGlueTest.m_nMaxCircle = m_nMaxCircle;
	gSystemMgr.m_sGlueTest.m_nCannyX = m_nCannyX;
	gSystemMgr.m_sGlueTest.m_nCannyY = m_nCannyY;
	gSystemMgr.m_sGlueTest.m_nDilateX = m_nDilateX;
	gSystemMgr.m_sGlueTest.m_nDilateY = m_nDilateY;
	gSystemMgr.m_sGlueTest.m_nMinRect = m_nMinRect;
	gSystemMgr.m_sGlueTest.m_nMaxRect = m_nMaxRect;
	gSystemMgr.m_sGlueTest.m_nDeviation = m_nDeviation;
	gSystemMgr.m_sGlueTest.m_nWidthCell = m_nWideCell;
	gSystemMgr.m_sGlueTest.m_nWidthFloor = m_nWidthFloor;
	gSystemMgr.m_sGlueTest.m_bSaveImage = m_bSaveImage;
	gSystemMgr.m_sGlueTest.m_bSaveImageOrg = m_bSaveImageOrg;
	gSystemMgr.m_sGlueTest.m_nCenterX = m_nCenterX;
	gSystemMgr.m_sGlueTest.m_nCenterY = m_nCenterY;
	gSystemMgr.m_sGlueTest.m_nThreShold = m_nThreShold;
	gSystemMgr.m_sGlueTest.m_nThreShold2 = m_nThreShold2;
	gSystemMgr.m_sGlueTest.m_bOffline = m_bOffline;
	gSystemMgr.m_sGlueTest.m_nBreakSpace = m_nBreakSpace;
	gSystemMgr.m_sGlueTest.m_nTestTimeout = m_nTestTimeout;
	gSystemMgr.m_sGlueTest.m_nCutBmpX = m_nCutBmpX;
	gSystemMgr.m_sGlueTest.m_nCutBmpY = m_nCutBmpY;
	gSystemMgr.m_sGlueTest.m_nCutBmpW = m_nCutBmpW;
	gSystemMgr.m_sGlueTest.m_nCutBmpH = m_nCutBmpH;

	gSystemMgr.m_sGlueTest.UserSetCircle = UserSetCircle;
	gSystemMgr.m_sGlueTest.SetCircleX = SetCircleX;
	gSystemMgr.m_sGlueTest.SetCircleY = SetCircleY;
	gSystemMgr.m_sGlueTest.SetCircleRMin = SetCircleRMin;
	gSystemMgr.m_sGlueTest.SetCircleRMax = SetCircleRMax;

	gSystemMgr.m_sGlueTest.TestCnt = TestCnt;

	CString cstr;
	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.UserSetCircle);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"UserSetCircle", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.SetCircleX);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"SetCircleX", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.SetCircleY);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"SetCircleY", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.SetCircleRMin);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"SetCircleRMin", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.SetCircleRMax);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"SetCircleRMax", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.TestCnt);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"TestCnt", cstr);


	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nCutBmpX);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nCutBmpX", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nCutBmpY);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nCutBmpY", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nCutBmpW);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nCutBmpW", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nCutBmpH);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nCutBmpH", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nTestTimeout);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nTestTimeout", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nBreakSpace);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nBreakSpace", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_bOffline);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_bOffline", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nThreShold);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nThreShold", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nThreShold2);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nThreShold2", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nMinCircle);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nMinCircle", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_bSaveImageOrg);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_bSaveImageOrg", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nCenterX);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nCircleCenterX", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nCenterY);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nCircleCenterY", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_bSaveImage);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_bSaveImage", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nMaxCircle);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nMaxCircle", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nCannyX);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nCannyValue1", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nCannyY);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nCannyValue2", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nDilateX);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nDilateX", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nDilateY);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nDilateY", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nMinRect);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nMinRect", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nMaxRect);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nMaxRect", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nDeviation);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nDeviation", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nWidthCell);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nGlueWidthMax", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nWidthFloor);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nGlueWidthMin", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nRingCannyValueX);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nRingCannyValueX", cstr);

	cstr.Format(L"%d", gSystemMgr.m_sGlueTest.m_nRingCannyValueY);
	gSystemMgr.m_cGlueTestInf.SaveConfigIni(L"GlueTest", L"m_nRingCannyValueY", cstr);

}

void CGlueTestDlg::ReadConfigToEditValue()
{
	m_nMinCircle = gSystemMgr.m_sGlueTest.m_nMinCircle;
	m_nMaxCircle = gSystemMgr.m_sGlueTest.m_nMaxCircle;
	m_nCannyX = gSystemMgr.m_sGlueTest.m_nCannyX;
	m_nCannyY = gSystemMgr.m_sGlueTest.m_nCannyY;
	m_nDilateX = gSystemMgr.m_sGlueTest.m_nDilateX;
	m_nDilateY = gSystemMgr.m_sGlueTest.m_nDilateY;
	m_nMinRect = gSystemMgr.m_sGlueTest.m_nMinRect;
	m_nMaxRect = gSystemMgr.m_sGlueTest.m_nMaxRect;
	m_nDeviation = gSystemMgr.m_sGlueTest.m_nDeviation;
	m_nWideCell = gSystemMgr.m_sGlueTest.m_nWidthCell;
	m_nWidthFloor = gSystemMgr.m_sGlueTest.m_nWidthFloor;
	m_nCenterX = gSystemMgr.m_sGlueTest.m_nCenterX;
	m_nCenterY = gSystemMgr.m_sGlueTest.m_nCenterY;
	m_nThreShold = gSystemMgr.m_sGlueTest.m_nThreShold;
	m_nThreShold2 = gSystemMgr.m_sGlueTest.m_nThreShold2;
	m_bSaveImage = gSystemMgr.m_sGlueTest.m_bSaveImage;
	m_bSaveImageOrg = gSystemMgr.m_sGlueTest.m_bSaveImageOrg;
	m_bOffline = gSystemMgr.m_sGlueTest.m_bOffline;
	m_nBreakSpace = gSystemMgr.m_sGlueTest.m_nBreakSpace;
	m_nTestTimeout = gSystemMgr.m_sGlueTest.m_nTestTimeout;
	m_nCutBmpX = gSystemMgr.m_sGlueTest.m_nCutBmpX;
	m_nCutBmpY = gSystemMgr.m_sGlueTest.m_nCutBmpY;
	m_nCutBmpW = gSystemMgr.m_sGlueTest.m_nCutBmpW;
	m_nCutBmpH = gSystemMgr.m_sGlueTest.m_nCutBmpH;

	UserSetCircle = gSystemMgr.m_sGlueTest.UserSetCircle;
	SetCircleX = gSystemMgr.m_sGlueTest.SetCircleX;
	SetCircleY = gSystemMgr.m_sGlueTest.SetCircleY;
	SetCircleRMin = gSystemMgr.m_sGlueTest.SetCircleRMin;
	SetCircleRMax = gSystemMgr.m_sGlueTest.SetCircleRMax;

	TestCnt = gSystemMgr.m_sGlueTest.TestCnt;

	UpdateData(FALSE);
}

void CGlueTestDlg::CompressImage(const Mat& src, OutputArray dst)
{
	//图像压缩
	int nCol = src.cols / 4;
	int nRow = src.rows / 4;
	cv::Mat matImage = cv::Mat::zeros(nCol, nRow, CV_8UC1);

	cv::Size dst_sz(nCol, nRow);
	cv::resize(src, dst, dst_sz);
}

int CGlueTestDlg::GlueTest(const Mat& matImage)
{
	CString cstrLog;
	USES_CONVERSION;
	int nResult = 0;
	m_Circle[0] = { 0,0,0 };
	m_Circle[1] = { 0,0,0 };

	if (matImage.empty())
	{
		nResult = 0;
		gSystemLog.DisplayLogAndSave(L"未检测到图", LV_ERROR);
		return nResult;
	}

	if (CV_8UC1 != matImage.type())
	{
		cvtColor(matImage, matImage, CV_BGR2GRAY);
	}
	m_bGlueWidthFlag = FALSE;
	m_matImage_S = matImage.clone();
	Mat TestImage = matImage.clone();

	GaussianBlur(TestImage, TestImage, cv::Size(9, 9), 3, 3);
	//二值化
	/*threshold(TestImage, TestImage, m_nThreShold, 255, 0);
	cstrLog.Format(L"mat threshold value:%d", m_nThreShold);
	gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);*/

	//边缘检测
	Canny(TestImage, TestImage, m_nCannyX, m_nCannyY, 3);
	cstrLog.Format(L"mat CannyX:%d,Canny:%d", m_nCannyX, m_nCannyY);
	gSystemLog.DisplayLogAndSave(cstrLog.GetBuffer(), LV_EVENT);

	//膨胀
	Mat element = getStructuringElement(MORPH_RECT, cv::Size(m_nDilateX, m_nDilateY), cv::Point(-1, -1)); //定义结构元素
	dilate(TestImage, TestImage, element);

	int j = 0;
	vector<vector<cv::Point>> contours;
	vector<Vec4i> hierarchy;

	findContours(TestImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

	vector<Point2f> center(contours.size());
	vector<float> r(contours.size());
	auto source = TestImage.clone();
	Mat roi = Mat::zeros(source.size(), CV_8UC1);
	Mat roi1 = Mat::zeros(source.size(), CV_8UC1);
	Mat roi2 = Mat::zeros(source.size(), CV_8UC1);
	gSystemLog.DisplayLogAndSave(L"start min Enclosing cicle", LV_EVENT);
	for (size_t i = 0; i < contours.size(); i++)
	{
		minEnclosingCircle(contours[i], center[i], r[i]);
	}
	gSystemLog.DisplayLogAndSave(L"min Enclosing cicle over", LV_EVENT);

	for (size_t i = 0; i < contours.size(); i++)
	{

		if (r[i] < m_nMaxCircle && r[i]>m_nMinCircle)
		{
			if (abs(center[i].y - m_nCenterY) < m_nDeviation && abs(center[i].x - m_nCenterX) < m_nDeviation)
			{
				m_Circle[j].x = center[i].x;
				m_Circle[j].y = center[i].y;
				m_Circle[j].r = r[i];

				if (j > 1)
				{
					if (abs(m_Circle[j].r - m_Circle[0].r) < 4)
					{
						m_Circle[1] = { 0,0,0 };
						j--;
					}
					else if (abs(m_Circle[1].r - m_Circle[j].r) > 5)
					{
						m_Circle[1].r = m_Circle[j].r;
						m_Circle[1].x = m_Circle[j].x;
						m_Circle[1].y = m_Circle[j].y;
					}
				}
				else
				{
					j++;
				}
			}
		}
	}
	if (abs(m_Circle[0].x - m_Circle[1].x) > 10 || abs(m_Circle[0].r - m_Circle[1].r) < 5 || m_Circle[1].r < 62)
	{
		CString cstr;
		cstr.Format(L"%d", abs(m_Circle[0].x - m_Circle[1].x));
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		cstr.Format(L"%d", abs(m_Circle[0].r - m_Circle[1].r));
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		cstr.Format(L"%d", m_Circle[1].r);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);


		cstr.Format(L"circle[%d] x:%d, y:%d,  r:%d", 1, int(m_Circle[0].x), int(m_Circle[0].y), m_Circle[0].r);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		cstr.Format(L"circle[%d] x:%d, y:%d,  r:%d", 2, int(m_Circle[1].x), int(m_Circle[1].y), m_Circle[1].r);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);

		gSystemLog.DisplayLogAndSave(L"无胶水NG", LV_ERROR);
		nResult = -1;
		m_fTestAgain = 0;
		return nResult;
	}

	if (m_Circle[0].r > m_Circle[1].r)
	{
		cv::Point centerB(m_Circle[0].x, m_Circle[0].y);
		cv::Point centerS(m_Circle[1].x, m_Circle[1].y);
		circle(m_matImage_S, centerB, 76, Scalar(0, 255, 0), 2);
		//circle(roi1, centerB, 308+20, CV_RGB(255, 255, 255), -1);
		circle(roi1, centerS, m_Circle[1].r - 11, CV_RGB(255, 255, 255), -1);
		circle(roi2, centerS, m_Circle[1].r - 5, CV_RGB(255, 255, 255), -1);

		circle(m_matImage_S, centerB, m_Circle[0].r, Scalar(0, 255, 0), 2);
		circle(m_matImage_S, centerS, m_Circle[1].r - 1, Scalar(0, 255, 0), 2);
		CString cstr;
		cstr.Format(L"circle[%d] x:%d, y:%d,  r:%d", 1, int(centerB.x), int(centerB.y), m_Circle[0].r);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		cstr.Format(L"circle[%d] x:%d, y:%d,  r:%d", 2, int(centerS.x), int(centerS.y), m_Circle[1].r);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

	}
	else
	{
		swap(m_Circle[0], m_Circle[1]);
		cv::Point centerB(m_Circle[0].x, m_Circle[0].y);
		cv::Point centerS(m_Circle[1].x, m_Circle[1].y);
		circle(m_matImage_S, centerB, 76, Scalar(0, 255, 0), 2);
		//circle(roi1, centerB, 308+20, CV_RGB(255, 255, 255), -1);
		circle(roi1, centerS, m_Circle[1].r - 11, CV_RGB(255, 255, 255), -1);
		circle(roi2, centerS, m_Circle[1].r - 5, CV_RGB(255, 255, 255), -1);

		circle(m_matImage_S, centerB, m_Circle[0].r, Scalar(0, 255, 0), 2);
		circle(m_matImage_S, centerS, m_Circle[1].r - 1, Scalar(0, 255, 0), 2);
		CString cstr;
		cstr.Format(L"circle[%d] x:%d, y:%d,  r:%d", 1, int(centerB.x), int(centerB.y), m_Circle[0].r);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		cstr.Format(L"circle[%d] x:%d, y:%d,  r:%d", 2, int(centerS.x), int(centerS.y), m_Circle[1].r);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
	}

	cv::subtract(roi2, roi1, roi);
	maskImage.release();
	matImage.copyTo(maskImage, roi);

	m_matImage_T = m_matImage_S.clone();
	m_matImage_S = maskImage.clone();
	Mat Image = maskImage.clone();

	//cvtColor(m_matImage_S, m_matImage_S, CV_BGR2GRAY);
	GaussianBlur(m_matImage_S, m_matImage_S, cv::Size(3, 3), 3, 3);
	Canny(m_matImage_S, m_matImage_S, gSystemMgr.m_sGlueTest.m_nRingCannyValueX, gSystemMgr.m_sGlueTest.m_nRingCannyValueY, 3);


	/****************************************************************/
	element = getStructuringElement(MORPH_RECT, cv::Size(2, 2), cv::Point(-1, -1)); //定义结构元素
	dilate(m_matImage_S, m_matImage_S, element); //膨胀


	findContours(m_matImage_S, contours, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
	RotatedRect box; //定义最小外接矩形

	vector<vector<cv::Point>>::iterator itc = contours.begin();
	Point2f rect_point[4];
	int area[2] = { 0 };
	int k = 0;
	for (; itc != contours.end(); itc++)
	{
		box = minAreaRect(Mat(*itc));  //计算每个轮廓最小外接矩形(旋转)
		box.points(rect_point);  //把最小外接矩形四个端点复制给rect数组
		if (box.size.area() > gSystemMgr.m_sGlueTest.m_nMinRect && box.size.area() < gSystemMgr.m_sGlueTest.m_nMaxRect)
		{
			double x = sqrt((rect_point[0].x - rect_point[1].x)*(rect_point[0].x - rect_point[1].x) + (rect_point[0].y - rect_point[1].y)*(rect_point[0].y - rect_point[1].y));
			double y = sqrt((rect_point[1].x - rect_point[2].x)*(rect_point[1].x - rect_point[2].x) + (rect_point[1].y - rect_point[2].y)*(rect_point[1].y - rect_point[2].y));

			if (x / y < 4 && x / y > 1 || y / x < 4 && y / x > 1)
			{
				for (int j = 0; j < 4; j++)
				{
					line(m_matImage_T, rect_point[j], rect_point[(j + 1) % 4], Scalar(0, 255, 0), 2, 8);  //绘制最小外接矩形每条边
					k++;
				}
			}

		}
	}
	if (k > 0)
	{
		nResult = -2;
		gSystemLog.DisplayLogAndSave(L"产品断胶NG", LV_ERROR);

	}
	else
	{
		MyGetLocation();//判断位置
		if (m_bGlueWidthFlag == TRUE)
		{
			nResult = -3;
			gSystemLog.DisplayLogAndSave(L"胶宽不达标NG", LV_ERROR);
		}
		else
		{
			nResult = true;
		}
	}
	m_fTestAgain = 0;
	gSystemMgr.GlueTestAcceptMes = "";
	gSystemMgr.m_matShow = m_matImage_T;
	return nResult;
}

void CGlueTestDlg::MyGetLocation()
{
	USES_CONVERSION;
	cv::Point A, B;
	const auto m_dLeft = abs(m_Circle[0].x - m_Circle[0].r - m_Circle[1].x + m_Circle[1].r);
	const auto m_dRight = abs(m_Circle[0].x + m_Circle[0].r - m_Circle[1].x - m_Circle[1].r);
	const auto m_dUp = abs(m_Circle[0].y - m_Circle[0].r - m_Circle[1].y + m_Circle[1].r);
	const auto m_dDown = abs(m_Circle[0].y + m_Circle[0].r - m_Circle[1].y - m_Circle[1].r);

	const auto x11 = m_Circle[0].x - sqrt(2) * 76 * 0.5;
	const auto x21 = m_Circle[1].x - sqrt(2)*(m_Circle[1].r - 10)* 0.5;
	const auto x12 = m_Circle[0].x + sqrt(2) * 76 * 0.5;
	const auto x22 = m_Circle[1].x + sqrt(2)*(m_Circle[1].r - 10) * 0.5;

	const auto y11 = m_Circle[0].y - sqrt(2) * 76 * 0.5;
	const auto y21 = m_Circle[1].y - sqrt(2)*(m_Circle[1].r - 10)* 0.5;
	const auto y12 = m_Circle[0].y + sqrt(2) * 76 * 0.5;
	const auto y22 = m_Circle[1].y + sqrt(2)*(m_Circle[1].r - 10)* 0.5;

	const auto m_dLeftUp = sqrt(pow(((m_Circle[0].x - sqrt(2) * m_Circle[0].r * 0.5) - (m_Circle[1].x - sqrt(2) * (m_Circle[1].r) * 0.5)), 2)
		+ pow(((m_Circle[0].y - sqrt(2) * m_Circle[0].r * 0.5) - (m_Circle[1].y - sqrt(2) * (m_Circle[1].r) * 0.5)), 2));
	const auto m_dLeftDowm = sqrt(pow(((m_Circle[0].x - sqrt(2) * m_Circle[0].r * 0.5) - (m_Circle[1].x - sqrt(2) * (m_Circle[1].r) * 0.5)), 2)
		+ pow(((m_Circle[0].y + sqrt(2) * m_Circle[0].r * 0.5) - (m_Circle[1].y + sqrt(2) *(m_Circle[1].r) * 0.5)), 2));
	const auto m_dRightUp = sqrt(pow(((m_Circle[0].x + sqrt(2) * m_Circle[0].r * 0.5) - (m_Circle[1].x + sqrt(2) * (m_Circle[1].r) * 0.5)), 2)
		+ pow(((m_Circle[0].y - sqrt(2) * m_Circle[0].r * 0.5) - (m_Circle[1].y - sqrt(2) * (m_Circle[1].r) * 0.5)), 2));
	const auto m_dRightDown = sqrt(pow(((m_Circle[0].x + sqrt(2) * m_Circle[0].r * 0.5) - (m_Circle[1].x + sqrt(2) * (m_Circle[1].r) * 0.5)), 2)
		+ pow(((m_Circle[0].y + sqrt(2) * m_Circle[0].r * 0.5) - (m_Circle[1].y + sqrt(2) *(m_Circle[1].r) * 0.5)), 2));;

	const auto LeftRight = m_dLeft - m_dRight;
	const auto UpDown = m_dUp - m_dDown;
	const auto LeftRightUp = m_dLeftUp - m_dRightDown;
	const auto RightLeftDown = m_dRightUp - m_dLeftDowm;

	CString cstr;
	string str;
	if (LeftRight >= 0)
	{
		A.x = m_Circle[0].x - 76;
		A.y = m_Circle[0].y;
		B.x = m_Circle[1].x - m_Circle[1].r;
		B.y = m_Circle[1].y;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"左侧胶水宽宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);

		A.x = m_Circle[0].x + 76 + LeftRight;
		A.y = m_Circle[0].y;
		B.x = m_Circle[0].x + m_Circle[1].r;
		B.y = m_Circle[0].y;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));


		cstr.Format(L"右侧胶水宽宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}

		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);

	}
	else
	{
		A.x = m_Circle[0].x - 76 - abs(LeftRight)*0.5;
		A.y = m_Circle[0].y;
		B.x = m_Circle[0].x - m_Circle[1].r;
		B.y = m_Circle[0].y;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"左侧胶水宽宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);

		A.x = m_Circle[0].x + 76;
		A.y = m_Circle[0].y;
		B.x = m_Circle[1].x + m_Circle[1].r;
		B.y = m_Circle[1].y;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"右侧胶水宽宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
	}



	if (UpDown >= 0)
	{
		A.x = m_Circle[0].x;
		A.y = m_Circle[0].y - 76;
		B.x = m_Circle[1].x;
		B.y = m_Circle[1].y - m_Circle[1].r;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"上方胶水宽宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);

		A.x = m_Circle[0].x;
		A.y = m_Circle[0].y + 76 + abs(UpDown)*0.5;
		B.x = m_Circle[0].x;
		B.y = m_Circle[1].y + m_Circle[1].r;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"下方胶水宽宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
	}
	else
	{
		A.x = m_Circle[0].x;
		A.y = m_Circle[0].y - 76 - abs(UpDown)*0.5;
		B.x = m_Circle[0].x;
		B.y = m_Circle[1].y - m_Circle[1].r;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"上方胶水宽宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}

		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);

		A.x = m_Circle[0].x;
		A.y = m_Circle[0].y + 76;
		B.x = m_Circle[1].x;
		B.y = m_Circle[1].y + m_Circle[1].r;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"下方胶水宽宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}

		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
	}



	if (LeftRightUp > 0)
	{
		A.x = x11;
		A.y = y11;
		B.x = x21;
		B.y = y21;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"左上方胶水宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);

		A.x = x12 + sqrt(2)*0.5*LeftRightUp;
		A.y = y12 + sqrt(2)*0.5*LeftRightUp;
		B.x = m_Circle[0].x + sqrt(2)*(m_Circle[1].r) * 0.5;
		B.y = m_Circle[0].y + sqrt(2)*(m_Circle[1].r) * 0.5;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"右下方胶水宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
	}
	else
	{
		A.x = x11 - sqrt(2)*0.25*abs(LeftRightUp);
		A.y = y11 - sqrt(2)*0.25*abs(LeftRightUp);
		B.x = m_Circle[0].x - sqrt(2)*(m_Circle[1].r) * 0.5;
		B.y = m_Circle[1].y - sqrt(2)*(m_Circle[1].r) * 0.5;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));


		cstr.Format(L"左上方胶水宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);

		A.x = x12;
		A.y = y12;
		B.x = x22;
		B.y = y22;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"右下方胶水宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
	}




	if (RightLeftDown > 0)
	{
		A.x = x12;
		A.y = y11;
		B.x = x22;
		B.y = y21;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"右上方胶水宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);

		A.x = x11 - sqrt(2)*0.25*RightLeftDown;
		A.y = y12 + sqrt(2)*0.25*RightLeftDown;
		B.x = m_Circle[0].x - sqrt(2)*(m_Circle[1].r) * 0.5;
		B.y = m_Circle[1].y + sqrt(2)*(m_Circle[1].r) * 0.5;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"左下方胶水宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
	}
	else
	{

		A.x = x12 + sqrt(2)*0.25*abs(RightLeftDown);
		A.y = y11 - sqrt(2)*0.25*abs(RightLeftDown);
		B.x = m_Circle[0].x + sqrt(2)*(m_Circle[1].r) * 0.5;
		B.y = m_Circle[1].y - sqrt(2)*(m_Circle[1].r) * 0.5;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"右上方胶水宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);

		A.x = x11;
		A.y = y12;
		B.x = x21;
		B.y = y22;
		line(m_matImage_T, A, B, Scalar(0, 255, 0));

		cstr.Format(L"左下方胶水宽度为:%.3f像素", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));

		if (sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) < gSystemMgr.m_sGlueTest.m_nWidthFloor ||
			sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)) > gSystemMgr.m_sGlueTest.m_nWidthCell)
		{
			m_bGlueWidthFlag = TRUE;
		}


		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		cstr.Format(L"%.3f", sqrt(pow((A.x - B.x), 2) + pow((A.y - B.y), 2)));
		str = T2A(cstr);
		putText(m_matImage_T, str, A, FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
	}

	cstr.Format(L"x偏移:%f像素", (m_Circle[0].x + m_Circle[1].x)*0.5 - gSystemMgr.m_sGlueTest.m_nCenterX);
	gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

	cstr.Format(L"y偏移:%f像素", (m_Circle[0].y + m_Circle[1].y)*0.5 - gSystemMgr.m_sGlueTest.m_nCenterY);
	gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

}


void CGlueTestDlg::OnBnClickedButtonHand()
{
	UpdateData(TRUE);
	if (m_matImage.empty())
	{
		return;
	}

	gSystemMgr.m_cGlueTest.m_nThreShold = m_nThreShold;
	gSystemMgr.m_cGlueTest.m_nThreShold2 = m_nThreShold2;
	gSystemMgr.m_cGlueTest.m_nDilateX = m_nDilateX;
	gSystemMgr.m_cGlueTest.m_nDilateY = m_nDilateY;
	gSystemMgr.m_cGlueTest.m_nCannyX = m_nCannyX;
	gSystemMgr.m_cGlueTest.m_nCannyY = m_nCannyY;
	gSystemMgr.m_cGlueTest.m_nCenterX = m_nCenterX;
	gSystemMgr.m_cGlueTest.m_nCenterY = m_nCenterY;
	gSystemMgr.m_cGlueTest.m_nDeviation = m_nDeviation;
	gSystemMgr.m_cGlueTest.m_nBreakSpace = m_nBreakSpace;
	gSystemMgr.m_cGlueTest.m_nWidthCell = m_nWideCell;
	gSystemMgr.m_cGlueTest.m_nWidthFloor = m_nWidthFloor;
	gSystemMgr.m_cGlueTest.m_nMinCircle = m_nMinCircle;
	gSystemMgr.m_cGlueTest.m_nMaxCircle = m_nMaxCircle;

	gSystemMgr.m_cGlueTest.m_nCutBmpX = m_nCutBmpX;
	gSystemMgr.m_cGlueTest.m_nCutBmpY = m_nCutBmpY;
	gSystemMgr.m_cGlueTest.m_nCutBmpW = m_nCutBmpW;
	gSystemMgr.m_cGlueTest.m_nCutBmpH = m_nCutBmpH;

	//固定识别到的圆的位置
	gSystemMgr.m_cGlueTest.UserSetCircle = UserSetCircle;
	gSystemMgr.m_cGlueTest.SetCircleX = SetCircleX;
	gSystemMgr.m_cGlueTest.SetCircleY = SetCircleY;
	gSystemMgr.m_cGlueTest.SetCircleRMin = SetCircleRMin;
	gSystemMgr.m_cGlueTest.SetCircleRMax = SetCircleRMax;


	gSystemMgr.m_cGlueTest.cnt = cnt;

	gSystemMgr.m_cGlueTest.GlueTest(m_matImage, m_matImage_S, m_matImage_2);
	gSystemMgr.ShowImage(m_matImage_S, this, IDC_SHOW_IMAGE);
	gSystemMgr.ShowImage(m_matImage_2, this, IDC_SHOW_IMAGE2);
	//gSystemMgr.ShowImage(m_matImage_S, gSystemMgr.m_ptrMainUI, IDC_SHOW_IMAGE);
	gSystemMgr.SaveTestResult(NULL, gSystemMgr.m_nGlueTestResult, m_matImage_S, m_matImage);
}



void CGlueTestDlg::OnBnClickedButtonOpen()
{
	IplImage* SrcImg = NULL;
	char *cPath = nullptr;
	CString cstrPath;
	CFileDialog dlg(TRUE, L"*.bmp;*.jpg;*.tif", NULL,
		OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
		L"image files All Files (*.*) |*.*||", NULL);// 选项图片的约定;  
	dlg.m_ofn.lpstrTitle = _T("打开图片");// 打开文件对话框的标题名;(*.bmp; *.jpg) |*.bmp; *.jpg |  
	if (dlg.DoModal() == IDOK)// 判断是否获得图片;  
	{

		if (dlg.GetFileExt() != "bmp" && dlg.GetFileExt() != "jpg"&&dlg.GetFileExt() != "tif"&&dlg.GetFileExt() != "png")
		{
			AfxMessageBox(_T("请选择正确的图片格式！"), MB_OK);
			return;
		}

		cstrPath = dlg.GetPathName();// 获取图片路径;  
		USES_CONVERSION;
		cPath = T2A(cstrPath);
		SrcImg = cvLoadImage(cPath);//读取图片、缓存到一个局部变量ipl中;  
		if (!SrcImg)// 判断是否成功载入图片;  
			return;
	}
	m_matImage = imread(cPath, IMREAD_COLOR); 
	cvtColor(m_matImage, m_matImage, CV_RGB2GRAY);
	//CompressImage(m_matImage, m_matImage);
	gSystemMgr.ShowImage(m_matImage, this, IDC_SHOW_IMAGE);
}

void CGlueTestDlg::OnBnClickedCheckOffline()
{
	UpdateData(TRUE);
	gSystemMgr.m_sGlueTest.m_bOffline = m_bOffline;
}


void CGlueTestDlg::OnBnClickedButton2()
{
	Point pointCenter(393, 276);
	//gSystemMgr.m_cGlueTest.FindGlueBreak(m_matImage, pointCenter, 171);
}
void OnMouseRect(int event, int x, int y, int flags, void* userdata)
{
	if (nullptr != m_cGlueTestDlg)
	{
		m_cGlueTestDlg->MouseRectangle(event, x, y, flags, userdata);
	}
}

void CGlueTestDlg::MouseRectangle(int event, int x, int y, int flags, void* userdata)
{
	if (event == EVENT_LBUTTONDOWN)
	{
		prePoint = { x,y };
	}
	else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))
	{
		cvtColor(m_matImage, m_matRect, CV_GRAY2RGB);
		rectangle(m_matRect, prePoint, Point(x, y), Scalar(0, 0, 255), 10);
		imshow("选取区域", m_matRect);
	}
	else if (!flags && event == CV_EVENT_LBUTTONUP)
	{
		m_nCutBmpX = prePoint.x;
		m_nCutBmpY = prePoint.y;
		m_nCutBmpW = x - prePoint.x;
		m_nCutBmpH = y - prePoint.y;
		UpdateData(FALSE);
		gSystemMgr.ShowImage(m_matRect, this, IDC_SHOW_IMAGE);
	}
}
void CGlueTestDlg::OnBnClickedButton1()
{
	if (m_matImage.empty())
	{
		return;
	}
	namedWindow("选取区域", WINDOW_NORMAL);
	resizeWindow("选取区域", 800, 800);
	imshow("选取区域", m_matImage);
	setMouseCallback("选取区域", OnMouseRect, 0);
}
